import serial
import threading

# 读压力传感器

# 全局变量，用于存储接收到的浮点数
global_data = 0.0
thread_flag = True

def read_from_serial(port, baudrate):
    global global_data, thread_flag
    ser = serial.Serial(port, baudrate)
    while True:
        try:
            line = ser.readline().decode(errors='ignore').strip()
            if line:
                try:
                    global_data = float(line)
                    #print(f"received value{line}")
                except ValueError as e:
                    print(f"Failed to convert to float: {e}")
        except serial.SerialException as e:
            print(f"Serial error: {e}")
            break

    ser.close()

def start_pressure_recv_svr():
    global thread_flag

    # 启动串口读取线程
    serial_thread = threading.Thread(target=read_from_serial, args=('COM6', 57600))
    serial_thread.daemon = True
    serial_thread.start()
    return serial_thread

def stop_pressure_recv_svr():
    global thread_flag
    thread_flag = False
